package com.gitee.W_Jun;

/**
 * @author https://gitee.com/W_Jun
 * @date 2021-12-19 23:31
 * @Description 虚拟机必须保证一个类的<clinit>()方法在多线程下被同步加锁
 */
public class DeadThreadTest {
    public static void main(String[] args) {
        Runnable r = () -> {
            System.out.println(Thread.currentThread().getName() + "开始");
            DeadThread dead = new DeadThread();
            System.out.println(Thread.currentThread().getName()+"结束");
        };
        Thread t1 = new Thread(r, "线程1");
        Thread t2 = new Thread(r, "线程2");
        t1.start();
        t2.start();
    }
}

class DeadThread {
    static {
        if (true) {
            //DeadThread只会初始化一次，只有一个线程抢到，把这个语句只执行一次
            System.out.println(Thread.currentThread().getName()+"初始化当前类");
            while (true) {

            }
        }
    }
}
